package com.example.service;

import com.alibaba.fastjson2.JSONObject;
import com.example.entity.Message;
import com.example.utils.BaiduConstant;
import okhttp3.*;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class MessageService {
      static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
      public static String getMessage(Message message) {
          MediaType mediaType = MediaType.parse("application/json");
          String s = JSONObject.toJSONString(message);
          System.out.println(s);
              RequestBody body = RequestBody.create(mediaType, s);
        Request request = new Request.Builder()
            .url("https://qianfan.baidubce.com/v2/app/conversation/runs")
            .method("POST", body)
            .addHeader("Content-Type", "application/json")
            .addHeader("X-Appbuilder-Authorization", BaiduConstant.Authorization)
            .build();
          try {
              Response response = HTTP_CLIENT.newCall(request).execute();

              System.out.println(response.body().string());
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
}
